import { create, update, deleteById, getById, queryList, approvalPurchase, reviewPurchase, queryApprovalStatement, getCstAmtById } from '../../services/capital/forexPurchase';
import {message} from 'antd';
const R=require("ramda");

export default {
    namespace: 'forexPurchase',
    state: {
        page: {},
        list: [],
        forexPurchase: {},
        appvoralRecordList: [],
        cstAmtList: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {
        /**
         * 分页查询购汇记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *queryList({payload}, { call, put, select }) {
            const res = yield call(queryList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取购汇详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    let dataSource = res.data.content;
                    yield put({
                        type: 'refreshState',
                        payload: {
                            forexPurchase: dataSource.data.forexPurchase
                        }
                    });
                    if(payload.onSuccess) payload.onSuccess();
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        forexPurchase: {}
                    }
                });
            }
        },

        /**
         * 删除购汇记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteById({ payload }, { call, put, select }) {
            const res = yield call(deleteById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                message.info("删除购汇记录成功");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 新建购汇记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *create({payload}, { call, put, select }) {
            const res = yield call(create, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("增加购汇记录成功");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 修改购汇记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *update({payload}, { call, put, select }) {
            const res = yield call(update, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("修改购汇记录成功");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 购汇审批
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *approvalPurchase({payload}, { call, put, select }) {
            const res = yield call(approvalPurchase, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("购汇记录审核成功");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 购汇审批记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *queryApprovalStatement({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(queryApprovalStatement, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    let dataSource = res.data.content;
                    yield put({
                        type: 'refreshState',
                        payload: {
                            appvoralRecordList: dataSource.list
                        }
                    });
                    if(payload.onSuccess) payload.onSuccess();
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        appvoralRecordList: []
                    }
                });
            }
        },

        /**
         * 购汇复核
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *reviewPurchase({payload}, { call, put, select }) {
            const res = yield call(reviewPurchase, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("购汇记录复核成功");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取购汇详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getCstAmtById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getCstAmtById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    let dataSource = res.data.content;
                    yield put({
                        type: 'refreshState',
                        payload: {
                            cstAmtList: dataSource
                        }
                    });
                    if(payload.onSuccess) payload.onSuccess();
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        cstAmtList: {}
                    }
                });
            }
        },

    }
};
